package com.model.objects;


import java.util.Iterator;
import java.util.List;

import com.model.logic.Field;
import com.model.logic.Location;

public class Jager implements Actor
{

    // Whether the hunter is alive or not.
    private boolean alive;
    // The hunters field.
    private Field field;
    // The hunters position in the field.
    private Location location;
    
    
    /**
     * Create a new jager at location in field.
     * 
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Jager(Field field, Location location)
    {
        alive = true;
        this.field = field;
        setLocation(location);
    }
    
    /**
     * Check whether the animal is alive or not.
     * @return true if the animal is still alive.
     */
    public boolean isAlive()
    {
        return alive;
    }

    /**
     * Indicate that the animal is no longer alive.
     * It is removed from the field.
     */
    protected void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }

    
    /**
     * Return the hunters location.
     * @return The hunters location.
     */
    protected Location getLocation()
    {
        return location;
    }
    
    /**
     * Place the animal at the new location in the given field.
     * @param newLocation The animal's new location.
     */
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
    
    /**
     * Return the animal's field.
     * @return The animal's field.
     */
    protected Field getField()
    {
        return field;
    }

    /**
     * This is what the hunter does most of the time: it hunts for
     * foxes. The number of hunters is assumed to be constant to start with.
     */
        @Override
        public void act( List<Actor> newJagers )
        {
                if(isAlive()) {        
            // Move towards a source of food if found.
            Location newLocation = findAnimals();
            if(newLocation == null) { 
                // No food found - try to move to a free location.
                newLocation = getField().randomAdjacentLocation(getLocation());
            }
            // See if it was possible to move.
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // stay where you are
            }
        }
                
        }
        
        /**
    * Look for rabbits or foxes adjacent to the current location.
    * Only the first live rabbit or fox is eaten.
    * @return Where food was found, or null if it wasn't.
    */
        private Location findAnimals()
        {
                Field field = getField();
                List<Location> adjacent = field.adjacentLocations( getLocation() );
                Iterator<Location> it = adjacent.iterator();
                while ( it.hasNext() )
                {
                        Location where = it.next();
                        Object animal = field.getObjectAt( where );
                        if( animal instanceof Rabbit )
                        {
                                Rabbit rabbit = (Rabbit) animal;
                                if( rabbit.isAlive() )
                                {
                                        rabbit.setDead();
                                        return where;
                                }
                        }
                        if( animal instanceof Fox )
                        {
                                Fox fox = (Fox) animal;
                                if( fox.isAlive() )
                                {
                                        fox.setDead();
                                        return where;
                                }
                        }
                }
                return null;
        }

}